<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="@/assets/logo2.png" alt="" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="loginOut">退出登录</el-button>
    </el-header>
    <!-- 主体区域 -->
    <el-container>
      <!-- 左侧边栏 -->
      <el-aside :width="toggle ? '64px' : '200px'">
        <!-- 展开收起按钮 -->
        <el-tooltip
          effect="dark"
          :enterable="false"
          :hide-after="2000"
          :content="toggle ? '展开' : '折叠'"
          placement="right"
        >
          <div class="toggle-button" @click="toggleCollapse">
            <i :class="toggle ? 'el-icon-s-unfold' : 'el-icon-s-fold'"></i>
          </div>
        </el-tooltip>
        <!-- 侧边栏菜单区域 -->
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409bff"
          unique-opened
          :collapse="toggle"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu :index="item.id + ''" v-for="item in menulist" :key="item.id">
            <!-- 一级菜单的模板 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconsObj[item.id]" style="color:#fff"></i>
              <!-- 文本 -->
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + i.path"
              v-for="i in item.children"
              :key="i.id"
              @click="savaNavState('/' + i.path)"
            >
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{ i.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容主体 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'Home',
  data() {
    return {
      // 菜单数组
      menulist: [],
      // 一级菜单图标对象
      iconsObj: {
        125: 'el-icon-user',
        103: 'el-icon-key',
        101: 'el-icon-shopping-bag-1',
        102: 'el-icon-document',
        145: 'el-icon-data-line'
      },
      // 是否折叠菜单
      toggle: false,
      // 被激活的链接地址
      activePath: ''
    }
  },
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    // 退出功能
    loginOut() {
      // 清空 token
      window.sessionStorage.clear()
      this.activePath = ''
      // 重定向到登录页面
      this.$router.push('/login')
    },
    // 获取左侧菜单数据
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      // 失败提示
      if (res.meta.status !== 200) {
        this.$message({
          type: 'error',
          message: res.meta.msg,
          center: true
        })
      }
      // 成功则赋值
      this.menulist = res.data
    },
    // 隐藏显示菜单文字
    toggleCollapse() {
      this.toggle = !this.toggle
    },
    // 保存链接的激活状态
    savaNavState(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
  color: #fff;

  .el-header {
    display: flex;
    background-color: #373d41;
    justify-content: space-between;
    padding-left: 0;
    align-items: center;
    font-size: 20px;
    > div {
      display: flex;
      align-items: center;

      img {
        width: 64px;
        margin-right: 10px;
        padding: 5px;
      }
    }
  }
  .el-aside {
    background-color: #333744;
    .el-menu {
      border-right: none;
    }
    .toggle-button {
      display: flex;
      height: 40px;
      background-color: #4a5064;
      text-align: right;
      align-items: center;
      cursor: pointer;
      i {
        flex: 1;
        margin-right: 24px;
      }
    }
  }
  .el-main {
    background-color: #eaedf1;
  }
}
</style>
